package com.spzx.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.product.domain.Category;
import com.spzx.product.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping("category")
@Tag(name = "商品分类管理模块")
public class CategoryController extends BaseController {
    @Autowired
    private CategoryService categoryService;

    @Operation(summary = "根据父id查询下一级商品分类列表")
    @GetMapping("/listTree/{pid}")
    public AjaxResult listTree(@PathVariable("pid") Long pid){
        return success(categoryService.selectTree(pid));
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response) throws UnsupportedEncodingException {
        categoryService.exportData(response);
    }
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        try {
            categoryService.importData(file);
            return AjaxResult.success("导入成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error("导入失败");
    }
}
